package service;

import dao.EquipmentMap;
import model.DataBatchModel;
import model.EquipmentDataModel;
import model.IniEquipmentDateModel;
import org.apache.ibatis.session.SqlSession;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * 提交数据库
 */
public class ToDbTask implements Runnable {
    private SqlSession sqlSession;
    private IniEquipmentDateModel iniEquipmentDateModel;

    public ToDbTask(SqlSession sqlSession, IniEquipmentDateModel iniEquipmentDateModel) {
        this.sqlSession = sqlSession;
        this.iniEquipmentDateModel = iniEquipmentDateModel;
    }

    @Override
    public void run() {
        if (!iniEquipmentDateModel.getDataBatchModels().isEmpty()) {
            EquipmentMap equipmentMap = sqlSession.getMapper(EquipmentMap.class);
            if (equipmentMap != null) {
                SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMM");
                int index = 0;
                final List<DataBatchModel> dataBatchModelsBySort = iniEquipmentDateModel.getDataBatchModelsBySort();
                final String tableNo = "_" + simpleDateFormat.format(new Date());
                for (DataBatchModel dataBatchModel :
                        dataBatchModelsBySort) {
                    EquipmentDataModel equipmentDataModel = new EquipmentDataModel();
                    equipmentDataModel.setTableNo(tableNo);
                    equipmentDataModel.setIp(iniEquipmentDateModel.getIp());
                    equipmentDataModel.setPort(iniEquipmentDateModel.getPort());
                    equipmentDataModel.setAddress(iniEquipmentDateModel.getAddress());
                    equipmentDataModel.setChannel(iniEquipmentDateModel.getChannel());
                    equipmentDataModel.setBatchId(String.format("%s_%s"
                            , simpleDateFormat.format(iniEquipmentDateModel.getDateNow())
                            , iniEquipmentDateModel.getBatchId()));
                    equipmentDataModel.setDate(iniEquipmentDateModel.getDateNow());
                    equipmentDataModel.setWaveValue(dataBatchModel.getWaveValue());
                    equipmentDataModel.setPowerValue(dataBatchModel.getPowerValue());
                    equipmentDataModel.setSortNo(index++ + 1);
                    switch (equipmentDataModel.getChannel()) {
                        case 1:
                            if (equipmentMap.updateEquipmentData1(equipmentDataModel) == 0)
                                equipmentMap.insertIntoIniDate1(equipmentDataModel);
                            break;
                        case 2:
                            if (equipmentMap.updateEquipmentData2(equipmentDataModel) == 0)
                                equipmentMap.insertIntoIniDate2(equipmentDataModel);
                            break;
                        case 3:
                            if (equipmentMap.updateEquipmentData3(equipmentDataModel) == 0)
                                equipmentMap.insertIntoIniDate3(equipmentDataModel);
                            break;
                        case 4:
                            if (equipmentMap.updateEquipmentData4(equipmentDataModel) == 0)
                                equipmentMap.insertIntoIniDate4(equipmentDataModel);
                            break;
                        case 5:
                            if (equipmentMap.updateEquipmentData5(equipmentDataModel) == 0)
                                equipmentMap.insertIntoIniDate5(equipmentDataModel);
                            break;
                        case 6:
                            if (equipmentMap.updateEquipmentData6(equipmentDataModel) == 0)
                                equipmentMap.insertIntoIniDate6(equipmentDataModel);
                            break;
                        case 7:
                        case 8:
                        case 9:
                        case 10:
                        case 11:
                        case 12:
                            System.out.println(String.format("通道%d需要写入数据...", equipmentDataModel.getChannel()));
                            break;
                        default:
                            if (equipmentMap.updateEquipmentData6(equipmentDataModel) == 0)
                                equipmentMap.insertIntoIniDate6(equipmentDataModel);
                            break;
                    }
                    sqlSession.commit();
//                    System.out.println(String.format("%d---->设备[%s:%d]地址:%d,通道%d,第%d条-----波长:%f,能量:%f---%s"
//                            , new Date().getTime()
//                            , equipmentDataModel.getIp()
//                            , equipmentDataModel.getPort()
//                            , equipmentDataModel.getAddress()
//                            , equipmentDataModel.getChannel()
//                            , equipmentDataModel.getSortNo()
//                            , equipmentDataModel.getWaveValue()
//                            , equipmentDataModel.getPowerValue()
//                            , equipmentDataModel.getBatchId()
//                    ));
                }
                iniEquipmentDateModel.Close();
            }
        }
    }
}

